package com.ruoyi.web.controller.gxs;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.gxs.domain.GxsECharts;
import com.ruoyi.gxs.domain.GxsGrade;
import com.ruoyi.gxs.domain.GxsJion;
import com.ruoyi.gxs.service.IGxsGradeService;
import com.ruoyi.gxs.service.IGxsJionService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gxs.domain.GxsPendActivity;
import com.ruoyi.gxs.service.IGxsPendActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 待审批活动Controller
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/gxs/activity")
public class GxsPendActivityController extends BaseController
{
    @Autowired
    private IGxsPendActivityService gxsPendActivityService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IGxsJionService gxsJionService;

    @Autowired
    private IGxsGradeService gxsGradeService;

    /**
     * 查询活动列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:list')")
    @GetMapping("/activityList")
    public TableDataInfo activityList(GxsPendActivity gxsPendActivity)
    {
        startPage();
        List<GxsPendActivity> list = gxsPendActivityService.selectGxsPendActivityList(gxsPendActivity);
        return getDataTable(list);
    }

    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        Long userId = getUserId();
        List<GxsPendActivity> list = gxsPendActivityService.notInSelectGxsPendActivityList(userId);

        Long deptId = getDeptId();
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        Long parentId = sysDept.getParentId();

//        Iterator<GxsPendActivity> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            GxsPendActivity activity = iterator.next();
//            if (!activity.getDeptId().equals(deptId) && !activity.getDeptId().equals(parentId)) {
//                // 根据 gxs_pend_activityid 查找 gxs_grade 表
//                GxsGrade gxsGrade = gxsGradeService.selectGxsGradeByPendActivityId(activity.getGxsPendActivityid());
//                Long aLong = gxsGrade.getGxsGrade();
//                if (aLong == 1 || aLong == 2) {
//                    // 如果 gxs_grade 为 1 或 2，则删除该元素
//                    iterator.remove();
//                }
//            }
//        }

        //创建了一个迭代器对象，用于遍历名为list的集合
        Iterator<GxsPendActivity> iterator = list.iterator();
        //iterator.hasNext()是一个方法，如果iterator还有下一个元素，它会返回true，否则返回false
        while (iterator.hasNext()) {
            //获取了iterator的下一个元素，并将其赋值给变量activity
            GxsPendActivity activity = iterator.next();
            GxsGrade gxsGrade = gxsGradeService.selectGxsGradeByPendActivityId(activity.getGxsPendActivityid());
            Long grade = gxsGrade.getGxsGrade();
            if (!activity.getDeptId().equals(deptId) && !activity.getParentId().equals(parentId)) {
                // 根据 gxs_pend_activityid 查找 gxs_grade 表
                if (grade != null && (grade == 1 || grade == 2)) {
                    // 如果 gxs_grade 为 1 或 2，则删除该元素
                    iterator.remove();
                }
            } else if (!activity.getDeptId().equals(deptId) && activity.getParentId().equals(parentId) && grade == 1) {
                // 如果 dept_id 和 parentId 相同，但和 deptId 不同，且 gxs_grade 为 1，则删除该元素
                iterator.remove();
            }
        }
        return getDataTable(list);
    }

    @GetMapping("/JoinList")
    public TableDataInfo JoinList()
    {
        startPage();
        Long userId = getUserId();
        List<GxsPendActivity> list = gxsPendActivityService.JoinSelectGxsPendActivityList(userId);

        return getDataTable(list);
    }

    /**
     * 导出待审批活动列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:export')")
    @Log(title = "待审批活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsPendActivity gxsPendActivity)
    {
        List<GxsPendActivity> list = gxsPendActivityService.selectGxsPendActivityList(gxsPendActivity);
        ExcelUtil<GxsPendActivity> util = new ExcelUtil<GxsPendActivity>(GxsPendActivity.class);
        util.exportExcel(response, list, "待审批活动数据");
    }

    /**
     * 获取待审批活动详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:query')")
    @GetMapping(value = "/{gxsPendActivityid}")
    public AjaxResult getInfo(@PathVariable("gxsPendActivityid") Long gxsPendActivityid)
    {
        return success(gxsPendActivityService.selectGxsPendActivityByGxsPendActivityid(gxsPendActivityid));
    }

    /**
     * 新增待审批活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:add')")
    @Log(title = "待审批活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsPendActivity gxsPendActivity)
    {
        //获取父部门id和部门id
        SysDept parentDept = sysDeptService.selectDeptById(getDeptId());
        gxsPendActivity.setParentId(parentDept.getParentId());
        gxsPendActivity.setDeptId(parentDept.getDeptId());

        //活动发起人
        gxsPendActivity.setCreateBy(getUsername());

        gxsPendActivity.setGxsPendState(0L);

        gxsPendActivityService.insertGxsPendActivity(gxsPendActivity);

        List<GxsPendActivity> list = gxsPendActivityService.selectGxsPendActivityList(gxsPendActivity);
        Long gxsPendActivityid = list.get(0).getGxsPendActivityid();
        String gxsPendDescription = gxsPendActivity.getGxsPendDescription();
        GxsGrade gxsGrade = new GxsGrade();
        if(gxsPendDescription.equals("限本班级"))
        {
            gxsGrade.setGxsPendActivityid(gxsPendActivityid);
            gxsGrade.setGxsGrade(1L);
        }
        if(gxsPendDescription.equals("限本学院"))
        {
            gxsGrade.setGxsPendActivityid(gxsPendActivityid);
            gxsGrade.setGxsGrade(2L);
        }
        if(gxsPendDescription.equals("限本学校"))
        {
            gxsGrade.setGxsPendActivityid(gxsPendActivityid);
            gxsGrade.setGxsGrade(3L);
        }
        return toAjax(gxsGradeService.insertGxsGrade(gxsGrade));
    }

    /**
     * 修改活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:edit')")
    @Log(title = "待审批活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsPendActivity gxsPendActivity)
    {
        return toAjax(gxsPendActivityService.updateGxsPendActivity(gxsPendActivity));
    }

    /**
     * 删除待审批活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:remove')")
    @Log(title = "待审批活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsPendActivityids}")
    public AjaxResult remove(@PathVariable Long[] gxsPendActivityids)
    {
        return toAjax(gxsPendActivityService.deleteGxsPendActivityByGxsPendActivityids(gxsPendActivityids));
    }


    /*
     * 查询已经审批
     * */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:list')")
    @GetMapping("/approvedList")
    public TableDataInfo approvedList(GxsPendActivity gxsPendActivity)
    {
        startPage();
        List<GxsPendActivity> list = gxsPendActivityService.selectPendApprovedList(gxsPendActivity);

        Long deptId = getDeptId();

        for (int i = 0 ; i < list.size(); i++){
            if (! (list.get(i).getParentId().equals(deptId))) {
                list.remove(i);
                i--;
            }
        }

        return getDataTable(list);
    }

    /*
     * 待审批活动
     * */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:list')")
    @GetMapping("/pendApprovedList")
    public TableDataInfo pendApprovedList(GxsPendActivity gxsPendActivity)
    {
        startPage();
        List<GxsPendActivity> list = gxsPendActivityService.selectApprovedListt(gxsPendActivity);
        Long deptId = getDeptId();

        for (int i = 0 ; i < list.size(); i++){
            if (! (list.get(i).getParentId().equals(deptId))) {
                list.remove(i);
                i--;
            }
        }

        return getDataTable(list);
    }

    /*我的活动*/
//    @PreAuthorize("@ss.hasPermi('gxs:activity:list')")
    @GetMapping("/MyList")
    public TableDataInfo MyList(GxsPendActivity gxsPendActivity)
    {
        startPage();
        List<GxsPendActivity> list = gxsPendActivityService.selectGxsPendActivityList(gxsPendActivity);

        String username = getUsername();

        for (int i = 0 ; i < list.size(); i++){
            String createBy = list.get(i).getCreateBy();
            if (!createBy.equals(username)) {
                list.remove(i);
                i--;
            }
        }
        return getDataTable(list);
    }

    /**
     * 提交活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:edit')")
    @Log(title = "提交活动", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{gxsPendActivityid}")
    public AjaxResult submit(@PathVariable Long gxsPendActivityid)
    {
        GxsPendActivity gxsPendActivity = gxsPendActivityService.selectGxsPendActivityByGxsPendActivityid(gxsPendActivityid);

        gxsPendActivity.setGxsPendState(1L);
        return toAjax(gxsPendActivityService.updateGxsPendActivity(gxsPendActivity));
    }

    /**
     * 举办活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:edit')")
    @Log(title = "举办活动", businessType = BusinessType.UPDATE)
    @PutMapping("/conduct")
    public AjaxResult conduct(@RequestBody GxsPendActivity gxsPendActivity)
    {
        return toAjax(gxsPendActivityService.updateGxsPendActivity(gxsPendActivity));
    }

    /**
     * 审批活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:edit')")
    @Log(title = "审批活动", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public AjaxResult approve(@RequestBody GxsPendActivity gxsPendActivity)
    {
        gxsPendActivity.setGxsPendState(2L);
        return toAjax(gxsPendActivityService.updateGxsPendActivity(gxsPendActivity));
    }


    /**
     * 本级活动分布
     */
//    @PreAuthorize("@ss.hasPermi('gxs:activity:list')")
    @GetMapping("/chartList")
    public TableDataInfo chartList(GxsPendActivity gxsPendActivity)
    {
        startPage();


        gxsPendActivity.setDeptId(getDeptId());
        List<GxsPendActivity> activityList = gxsPendActivityService.selectGxsPendActivityList(gxsPendActivity);

        List<GxsECharts> list = new ArrayList<>();
        GxsECharts eCharts0 = new GxsECharts();
        GxsECharts eCharts1 = new GxsECharts();
        GxsECharts eCharts2 = new GxsECharts();

        eCharts0.setType("专业技能");
        eCharts0.setNum(0);;
        list.add(0,eCharts0);
        eCharts1.setType("思想品德");
        eCharts1.setNum(0);;
        list.add(1, eCharts1);
        eCharts2.setNum(0);;
        eCharts2.setType("体美劳");
        list.add(2,eCharts2);

        //筛选本级活动
        Long deptId = getDeptId();
//
//        for (int i = 0 ; i < activityList.size(); i++){
//            if (! (activityList.get(i).getParentId().equals(deptId))) {
//                list.remove(i);
//                i--;
//            }
//        }

        for (int i = 0 ; i < activityList.size(); i++){
            Long gxsTypeId = activityList.get(i).getGxsTypeId();
            switch ( gxsTypeId.intValue()){
                case 0:
                    Integer num0 = list.get(0).getNum();
                    list.get(0).setNum( num0 + 1);
                    break;
                case 1:
                    Integer num1 = list.get(1).getNum();
                    list.get(1).setNum( num1 + 1);
                    break;
                case 2:
                    Integer num2 = list.get(2).getNum();
                    list.get(2).setNum( num2 + 1);
                    break;
            }
        }

        return getDataTable(list);
    }
}
